
import { _decorator, Component, director, Prefab, instantiate, find, Event, NodePool, view, math } from 'cc';
import { Game } from './Game';
const { ccclass, property } = _decorator;
 
@ccclass('Menu')
export class Menu extends Component {

    @property(Prefab)
    Bubble: Prefab = null;

    @property(Prefab)
    Fish1: Prefab = null;

    @property(Prefab)
    Fish2: Prefab = null;

    @property(Prefab)
    Fish3: Prefab = null;

    @property(Prefab)
    Fish4: Prefab = null;

    @property(Prefab)
    Fish5: Prefab = null;

    @property(Prefab)
    Fish6: Prefab = null;

    @property(Prefab)
    Fish7: Prefab = null;
    
    @property(Prefab)
    Fish8: Prefab = null;

    @property(Prefab)
    Fish9: Prefab = null;

    @property(Prefab)
    Fish10: Prefab = null;

    BubbleNode = null;
    FishNode = null;
    interval = 0;
    bubblePool: NodePool;
    _height = 0;
    bubbleX = [-200, -120, 120, 200];
    
    onLoad() {
        this._height = view.getVisibleSize().height;
        director.preloadScene("Game");
    }

    start() {
        this.BubbleNode = instantiate(this.Bubble);
        const bgImg = find('/Canvas/bg');
        const Fish = find('/Canvas/Fish');
        const fishRdm = parseInt(`${math.random() * 10 + 1}`);
        this.FishNode = instantiate(this[`Fish${fishRdm}`]);
        bgImg.addChild(this.BubbleNode);
        Fish.addChild(this.FishNode);
    }

    onClickLyric(event: Event, type: string) {
        director.loadScene("Game", (e, s) => {
            s.getChildByName('Game').getComponent(Game).type = parseInt(type);
            console.log(s.getChildByName('Game').getComponent(Game).type)
        });
    }

    update(dt) {
        this.BubbleNode.setPosition(this.BubbleNode.position.x, this.BubbleNode.position.y + 8);
        if(this.BubbleNode.position.y >= this._height / 2) {
            this.BubbleNode.setPosition(this.bubbleX[parseInt(`${math.random() * 4}`)], -(this._height / 2 + 2000))
        }
    }
}
